package com.mycompany.myapp.web.rest.controller;

import com.mycompany.myapp.domain.SUser;
import com.mycompany.myapp.service.MailService;
import com.mycompany.myapp.service.SUserService;
import com.mycompany.myapp.service.dto.SUserDTO;
import com.mycompany.myapp.service.dto.SUserInfoDTO;
import com.mycompany.myapp.web.rest.errors.EmailAlreadyUsedException;
import com.mycompany.myapp.web.rest.errors.InvalidPasswordException;
import com.mycompany.myapp.web.rest.errors.LoginAlreadyUsedException;
import com.mycompany.myapp.web.rest.utils.ResultObj;
import com.mycompany.myapp.web.rest.vm.KeyAndPasswordVM;
import com.mycompany.myapp.web.rest.vm.ManagedUserVM;
import com.mycompany.myapp.web.rest.vm.RegisterUserVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;


@Api(tags = {"用户管理"})
@RestController
@RequestMapping("/api")
public class SUserController {

    private final SUserService sUserService;

    private final MailService mailService;

    public SUserController(SUserService sUserService, MailService mailService) {
        this.sUserService = sUserService;
        this.mailService = mailService;
    }

    @GetMapping("/userInfo")
    public ResultObj getAllUserInfo(){
        return ResultObj.back(200,sUserService.findAllSUser());
    }

    @GetMapping("/user/{phone}")
    public ResultObj getUserInfoByPhone(@PathVariable String phone){
        return ResultObj.back(200,sUserService.findOneByPhone(phone).map(SUserInfoDTO::new));
    }

    /**
     * {@code POST  /register} : register the user.
     *
     * @param registerUserVM the managed user View Model.
     * @throws InvalidPasswordException {@code 400 (Bad Request)} if the password is incorrect.
     * @throws EmailAlreadyUsedException {@code 400 (Bad Request)} if the email is already used.
     * @throws LoginAlreadyUsedException {@code 400 (Bad Request)} if the login is already used.
     */
    @PostMapping("/user/register")
    @ResponseStatus(HttpStatus.CREATED)
    public ResultObj registerAccount(@RequestBody RegisterUserVM registerUserVM) {
       /* if (!checkPasswordLength(registerUserVM.getPassword())) {
            throw new InvalidPasswordException();
        }*/
        //SUser user = sUserService.registerUser(registerUserVM, registerUserVM.getPassword());
        // mailService.sendActivationEmail(user);
        return sUserService.registerUser(registerUserVM, registerUserVM.getPassword());
    }

    private static boolean checkPasswordLength(String password) {
        return !StringUtils.isEmpty(password) &&
            password.length() >= ManagedUserVM.PASSWORD_MIN_LENGTH &&
            password.length() <= ManagedUserVM.PASSWORD_MAX_LENGTH;
    }

    @ApiOperation("向邮箱发送验证码")
    @GetMapping("/user/sendEmail")
    public ResultObj sendEmail(@RequestParam String Email, HttpSession session){
        return sUserService.sendEmail(Email,session);
    }

    @ApiOperation("根据验证码修改密码")
    @PostMapping("/user/alterPassword")
    public ResultObj alterPassword (@Valid @RequestBody KeyAndPasswordVM keyAndPasswordVM , HttpSession session){
        return sUserService.alterPassword(keyAndPasswordVM.getKey(),keyAndPasswordVM.getNewPassword(),session);
    }

    @ApiOperation("修改密码")
    @PostMapping("/user/changePassword")
    public ResultObj changePassword (@ApiParam(name = "userId",value = "用户Id",required = true)@RequestParam Long userId,
                                     @ApiParam(name="password",value="密码",required=true) @RequestParam String password) {
        return sUserService.changePassword(userId, password);
    }

    @ApiOperation("验证邮箱是否注册")
    @GetMapping("/user/checkemail")
    public ResultObj checkemail(@RequestParam String email){
        return sUserService.checkEmail(email);
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/user/update")
    public ResultObj updateuserInfo(@RequestBody SUserDTO sUserDTO){
        return sUserService.updateSUser(sUserDTO);
    }

    @ApiOperation("用户登陆 RequestParam")
    @PostMapping("/suser/login")
    public ResultObj findSUser(@ApiParam(name="username",value="用户名或者邮箱",required=true) @RequestParam String username,
                               @ApiParam(name="password",value="密码",required=true) @RequestParam String password) {
        return sUserService.findSUser(username, password);
    }

    @ApiOperation("查询用户信息 RequestParam")
    @PostMapping("/suser/select")
    public ResultObj findSUser(@ApiParam(name="username",value="仅用户名",required=true) @RequestParam String username) {
        return sUserService.findSUserByUsername(username);
    }

}
